package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.AuditReceivableAccount;
import com.xmy.cultivate.entity.Department;
import com.xmy.cultivate.service.IAuditReceivableAccountService;
import com.xmy.cultivate.service.IDepartmentService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 收款帐号 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-01-01
 */
@RestController
@RequestMapping("/api/auditReceivableAccount")
public class AuditReceivableAccountController extends BaseController {
    @Autowired
    IAuditReceivableAccountService iAuditReceivableAccountService;


    @GetMapping("")
    @ApiOperation("获取收款账号")
    public Result getAccount(@RequestParam(value = "payType", required = false) String payType,
                             @RequestParam(value = "teacherId", required = false) String teacherId // 报销人id
    ) {

        Long adminId = this.adminId;
        if (StringUtils.isNotBlank(teacherId)) {
            adminId = Long.valueOf(teacherId);
        }

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id", adminId);
        queryWrapper.eq(StringUtils.isNotBlank(payType), "pay_type", payType);

        List<AuditReceivableAccount> accountList = iAuditReceivableAccountService.list(queryWrapper);

        return new Result(Code.OK, accountList, Code.GET_OK_MSG);
    }

    @PostMapping("")
    @ApiOperation("添加、修改收款账号")
    public Result addAccount(@RequestBody AuditReceivableAccount auditReceivableAccount) {
        auditReceivableAccount.setTeacherId(this.adminId);
        iAuditReceivableAccountService.saveOrUpdate(auditReceivableAccount);
        return new Result(Code.OK, "", Code.SAVE_OK_MSG);
    }

    @DeleteMapping("")
    @ApiOperation("删除收款账号")
    public Result delAccount(@RequestParam(value = "id") String id) {
        AuditReceivableAccount auditReceivableAccount = iAuditReceivableAccountService.getById(id);
        if (Objects.isNull(auditReceivableAccount)) {
            return new Result(Code.EXP_ERROR, null, "该收款账号不存在");
        }

        iAuditReceivableAccountService.removeById(id);

        return new Result(Code.OK, "", Code.DELETE_OK_MSG);
    }


}
